# -*- coding:utf-8 -*-
"""
Created on 2010-1-20

@author: Administrator
"""
#from random import random
from module import *
from core import Trans,HGraph #,ConnectedComponet


if __name__ =="__main__":
    
    def output(trans):
        # echo the transmission
        print '\nTrans is:','(',len(trans.modules),len(trans.relations),')'
        print trans
        nRelations = trans.getnRelations()
        print '\nnRelations is:',len(nRelations),'\n',nRelations
        vertics = trans.getPorts()
        print '\nPorts is:',len(vertics),'\n',vertics
    
                    
    '''
    one typical trans
    '''
    e = Engine('e')
    tc = Converter('tc')
    f = Gear3('f')
    b1 = Gear2('b1')
    p1 = Planet('p1')
    p2 = Planet('p2')
    z = Brake('z')
    c = Clutch('c'); #c.setName('8')
    b2 = Gear2(); b2.setName('b2')
    p3 = Planet(); p3.setName('b3')
#    b2 = Gear2('9')
#    p3 = Planet('10')
    trans = Trans(e,tc,f,b1,p1,p2,z,c,b2)
    trans.addModule(p3)

    # add relation between modules
    r1 = Relation(e.getPort('e'),tc.getPort('p'))
    r2 = Relation(tc.getPort('w'),f.getPort('i'))
    r3 = Relation(f.getPort('o1'),b1.getPort('i'))
    r4 = Relation(f.getPort('o2'),b2.getPort('i'))
    r5 = Relation(b2.getPort('o'),p3.getPort('t'))
    r6 = Relation(b1.getPort('o'),p1.getPort('t'))
    r7 = Relation(p1.getPort('j'),c.getPort('u'))
    r8 = Relation(p1.getPort('q'),c.getPort('v'))
    r9 = Relation(p1.getPort('q'),z.getPort('z'))
    r10 = Relation(p1.getPort('j'),p2.getPort('q'))
    r11 = Relation(p1.getPort('t'),p2.getPort('j'))
    r12 = Relation(p2.getPort('t'),p3.getPort('q'))
    trans.addRelation(r1,r2,r3)
    trans.addRelation(r4,r5,r6,r7,r8,r9,r10,r11,r12)
    
    # control modules is working
    c.clutch(True)
    z.brake(False)
    tc.lock(False)
    output(trans)
    
    graph = HGraph(trans)
    print '\n------------------------------\n'
    print '\nGraph Nodes:',len(graph.nodes),'\n',graph.nodes
    print '\nGraph nEdges:',len(graph.nEdges),'\n',graph.nEdges
    print '\nGraph mEdges:',len(graph.mEdges),'\n',graph.mEdges
    for node in graph.nodes:
        print '\n',node
        print 'edges:', node.edges
    for edge in graph.nEdges:
        print edge,
    
    src = graph.getNode(e.getPort('e'))
    dest = graph.getNode(p3.getPort('q'))
    
#    graph.findnPath(src, dest)
    
    trans.removeModule(e,tc,p3)
#    
    output(trans)
    
    

    '''
    end of typical trans
    '''

#    trans = typical()
    
#    c = ConnectedComponet(trans)
#    c.getConnComps()

#def typical():
#    # construct the transmission using modules
#    e = Engine('1')
#    tc = Converter('2')
#    f = Gear3('3')
#    b1 = Gear2('4')
#    p1 = Planet('5')
#    p2 = Planet('6')
#    z = Brake('7')
#    c = Clutch('8'); #c.setName('8')
#    b2 = Gear2(); b2.setName('9')
#    p3 = Planet(); p3.setName('10')
#    typical = Trans(e,tc,f,b1,p1,p2,z,c,b2)
#    typical.addModule(p3)
#
#    # add relation between modules
#    r1 = Relation(e,e.getPort('e'),tc,tc.getPort('p'))
#    r2 = Relation(tc,tc.getPort('w'),f,f.getPort('i'))
#    r3 = Relation(f,f.getPort('o1'),b1,b1.getPort('i'))
#    r4 = Relation(f,f.getPort('o2'),b2,b2.getPort('i'))
#    r5 = Relation(b2,b2.getPort('o'),p3,p3.getPort('t'))
#    r6 = Relation(b1,b1.getPort('o'),p1,p1.getPort('t'))
#    r7 = Relation(p1,p1.getPort('j'),c,c.getPort('u'))
#    r8 = Relation(p1,p1.getPort('q'),c,c.getPort('v'))
#    r9 = Relation(p1,p1.getPort('q'),z,z.getPort('z'))
#    r10 = Relation(p1,p1.getPort('j'),p2,p2.getPort('q'))
#    r11 = Relation(p1,p1.getPort('t'),p2,p2.getPort('j'))
#    r12 = Relation(p2,p2.getPort('t'),p3,p3.getPort('q'))
#    typical.addRelation(r1,r2,r3)
#    typical.addRelation(r4,r5,r6,r7,r8,r9,r10,r11,r12)
#    
#    # control modules is working
#    c.clutch(True)
#    z.brake(False)
##    tc.lock(True)
#    
#    # echo the transmission
#    print typical
#    nRelations = typical.getnRelations()
#    print nRelations,'\n',len(nRelations)
#    vertics = typical.getPorts()
#    print vertics,'\n',len(vertics)
#        
#        
#
#def typicalTrans():
#    vertics = {}
#    for i in range(22):
#        name = chr(i+ord('a'))
#        vertics[name] = Port(name,random())
##    print(vertics)
#    
#    modules = {}
#    i = 1
#    modules[i] = Module(vertics['a'])
#    modules[i].addnRelations(Relation(vertics['a'],vertics['a']))
#    
#    i+=1
#    modules[i] = Module(vertics['b'],vertics['c'])
#    modules[i].addnRelations(Relation(vertics['b'],vertics['c']))
#
#    i+=1    
#    modules[i] = Module(vertics['d'],vertics['e'],vertics['f'])
#    modules[i].addnRelations(Relation(vertics['d'],vertics['e']),Relation(vertics['d'],vertics['f']))
#
#    i+=1
#    modules[i] = Module(vertics['g'],vertics['h'])
#    modules[i].addnRelations(Relation(vertics['g'],vertics['h']))
#
#    i+=1
#    modules[i] = Module(vertics['i'],vertics['j'],vertics['k'])
#    modules[i].addnRelations(Relation(vertics['i'],vertics['j'],vertics['k']))
#
#    i+=1
#    modules[i] = Module(vertics['l'],vertics['m'],vertics['n'])
#    modules[i].addnRelations(Relation(vertics['l'],vertics['m'],vertics['n']))
#
#    i+=1
#    modules[i] = Module(vertics['o'])
#    modules[i].addnRelations(Relation(vertics['o']))
#
#    i+=1
#    modules[i] = Module(vertics['p'],vertics['q'])
#    modules[i].addnRelations(Relation(vertics['p'],vertics['q']))
#
#    i+=1
#    modules[i] = Module(vertics['r'], vertics['s'], vertics['t'])
#    modules[i].addnRelations(Relation(vertics['r'],vertics['s'],vertics['t']))
#
#    i+=1
#    modules[i] = Module(vertics['u'], vertics['v'])
#    modules[i].addnRelations(Relation(vertics['u'],vertics['v']))
#    
#    trans = Trans(modules)
##    trans.addModule({11:modules[1]})
#    
#    trans.addRelation((modules[1],vertics['a']), (modules[2],vertics['b']))
#    trans.addRelation((modules[2],vertics['c']), (modules[3],vertics['d']))
#    trans.addRelation((modules[3],vertics['e']), (modules[4],vertics['g']))
#    trans.addRelation((modules[3],vertics['f']), (modules[10],vertics['u']))
#    trans.addRelation((modules[4],vertics['h']), (modules[5],vertics['i']))
#    trans.addRelation((modules[5],vertics['j']), (modules[6],vertics['n']))
#    trans.addRelation((modules[5],vertics['i']), (modules[6],vertics['n']))
#    trans.addRelation((modules[5],vertics['j']), (modules[8],vertics['p']))
#    trans.addRelation((modules[5],vertics['k']), (modules[7],vertics['o']))
#    trans.addRelation((modules[6],vertics['l']), (modules[9],vertics['r']))
#    trans.addRelation((modules[10],vertics['v']), (modules[9],vertics['s']))
##    if debug:
##        for i,j in trans.items():
##            print 'm'+str(i)+': ',j
##        
##    modules = []
##    for i in range(1,11):
##        modules.append(eval('m'+str(i))) # locals()['a'], vars()['a'], eval('a')
##    modules = tuple(modules)
##    print(modules)
#    c = Clutch()
#    c.clutch(True)
#    print c
#    c.clutch(False)
#    print c
#    c.clutch(True)
#    print c
#    
#    z = Brake()
#    z.brake(True)
#    print z
#    z.brake(False)
#    print z
#    z.brake(True)
#    print z  
##    return trans

